﻿using FbHelperWP7NonUi.BookStorage.PhoneStorage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using FbHelperWP7NonUi.TextHelpers;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Text;

namespace FbHelperNonUiTests
{
    [TestClass()]
    public class PhoneDirectoryTest
    {
        [TestMethod()]
        public void DirectoryTest()
        {
            PhoneDirectory root = PhoneDirectory.Root;

            List<string> children = root.GetSubdirectories();

            Assert.IsNotNull( children );

            string directoryName = Guid.NewGuid().ToString();

            root.AddDirectory( directoryName );

            children = root.GetSubdirectories();

            Assert.IsNotNull( children );

            CollectionAssert.Contains( children, directoryName );

            root.RemoveDirectory( directoryName );

            children = root.GetSubdirectories();

            CollectionAssert.DoesNotContain( children, directoryName );
        }

        /// <summary>
        ///A test for AddFile
        ///</summary>
        [TestMethod()]
        public void FileTest()
        {
            PhoneDirectory root = PhoneDirectory.Root;

            List<string> files = root.GetFiles();

            Assert.IsNotNull( files );

            string fileName = Guid.NewGuid().ToString();

            root.AddFile( fileName, new byte[0] );

            files = root.GetFiles();

            Assert.IsNotNull( files );

            CollectionAssert.Contains( files, fileName );

            root.RemoveFile( fileName );

            files = root.GetFiles();

            CollectionAssert.DoesNotContain( files, fileName );
        }

        [TestMethod]
        public void OpenFileTest()
        {
            PhoneDirectory root = PhoneDirectory.Root;

            string directoryName = "Directory." + Guid.NewGuid().ToString();

            root.AddDirectory( directoryName );

            PhoneDirectory targetDirectory = root.OpenDirectory( directoryName );
            string fileName = "File." + Guid.NewGuid().ToString() + ".tXt";

            targetDirectory.AddFile( fileName, Encoding.UTF8.GetBytes( "Some text\n\nsdlfgsjlg" ) );

            Book book = targetDirectory.OpenFile( fileName );

            Assert.IsNotNull( book );
            Assert.IsInstanceOfType( book, typeof( TextBook ) );            
        }

        /// <summary>
        ///A test for OpenDirectory
        ///</summary>
        [TestMethod()]
        public void OpenDirectoryTest()
        {
            PhoneDirectory root = PhoneDirectory.Root;

            string directoryName = Guid.NewGuid().ToString();

            root.AddDirectory( directoryName );

            PhoneDirectory subDirectory = root.OpenDirectory( directoryName );

            Assert.IsNotNull( subDirectory );
            Assert.AreEqual( directoryName, subDirectory.Name );
            Assert.AreEqual( root, subDirectory.Parent );

            PhoneDirectory subDirectory2 = root.OpenDirectory( directoryName );

            Assert.AreEqual( subDirectory, subDirectory2 );
        }

        /// <summary>
        ///A test for Root
        ///</summary>
        [TestMethod()]
        public void RootTest()
        {
            PhoneDirectory root = PhoneDirectory.Root;
            Assert.IsNotNull( root );
            Assert.IsNotNull( root.Name );
            Assert.IsNull( root.Parent );
        }
    }
}
